<template>
  <div class="login">
    <div class="login-wrapper">
      <div class="login-title">运营后台-NEW</div>
			<div class="sub-title">欢迎使用 好生活</div>
      <div class="login-content">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="0" class="demo-ruleForm">
          <el-form-item label="" prop="name">
            <el-input v-model="loginForm.name" placeholder="用户名" :maxlength="20" ref="userNameInput" @keyup.native.enter="submitForm"></el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="密码" :maxlength="20" @keyup.native.enter="submitForm"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm" class="login-btn">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  import api from '@/api/apiPath'
  export default {
    name: 'login',
    data () {
      return {
        loginForm: {
          name: '',
          password: ''
        },
        rules: {
          name: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      submitForm () {
        this.$refs['loginForm'].validate((valid) => {
          if (valid) {
            let param = {}
            param.loginName = this.loginForm.name
            param.password = this.loginForm.password
            param.loginSysName = 'cms'
            param.loginType = 2
            axios.post(api.LOGIN , param).then(res => {
              const data = res.data
              if (data.code === 200) {
                let sysUser = {}
                sysUser.name = data.data.userName
                sysUser.tokenId = data.data.token
                sysUser.userId = data.data.id
                sysUser.cityId = data.data.extend.cityId
                sysUser.roleName = data.data.extend.roleName
                sysUser.mobile = data.data.loginName;
                const menuTemp = data.data.menus
                let menuList = []
                menuTemp.forEach((yiji,ind) => {
                  menuList.push({index: String(yiji.id), img: yiji.menuIcon, title: yiji.menuName})
                  if (yiji.submenus && yiji.submenus.length>0){
                    menuList[ind].children = []
                    yiji.submenus.forEach((erji,index) => {
                      menuList[ind].children.push({url: erji.menuUrl, title: erji.menuName})
                      if (erji.submenus && erji.submenus.length>0) {
                          menuList[ind].children[index].children = []
                          erji.submenus.forEach((sanji,idex) =>{
                            menuList[ind].children[index].children.push({url: sanji.menuUrl, title: sanji.menuName})
                          })
                      }
                    })
                  }
                })
                sysUser.menu = JSON.stringify(menuList)
                window.sessionStorage.cmsUser = JSON.stringify(sysUser)
                window.localStorage.cmsUser = JSON.stringify(sysUser)
                this.$router.push({path: '/'})
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style scoped lang="stylus">
  .login{
    height: 100%;
    position: relative;
    .login-wrapper{
      position: absolute;
      width: 400px;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      .login-title{
        text-align: center;
        font-size: 70px;
				font-weight: bold;
				color: #e6e6e6;
        white-space: nowrap;
      }
			.sub-title{
				text-align: center;
				font-weight: bold;
			}
      .login-content{
        padding: 30px;
        box-sizing: border-box;
        background: #fff;
        border-radius: 5px;
        .login-btn{
          width: 100%;
        }
      }
    }
  }
</style>
